package com.sjd.storehouse.controller;

import com.sjd.storehouse.entity.AjaxRespInfo;
import com.sjd.storehouse.entity.AssistList;
import com.sjd.storehouse.entity.Center;
import com.sjd.storehouse.entity.Rent;
import com.sjd.storehouse.service.StoreService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/storeController")
public class StoreController {

    @Resource
    private StoreService storeService;

    @RequestMapping("/addCenter")
    @ResponseBody
    AjaxRespInfo addCenter(Center center) {
        AjaxRespInfo ajaxRespInfo = new AjaxRespInfo();
        int rowNum = storeService.addCenter(center);
        if (rowNum == 1) {
            ajaxRespInfo.setCode(0);
            ajaxRespInfo.setMsg("添加成功!");
        } else {
            ajaxRespInfo.setCode(-1);
            ajaxRespInfo.setMsg("添加失败!");
        }
        return ajaxRespInfo;
    }

    /**
     * 新增二级库 需要审核 默认del_flag为2
     */
    @RequestMapping("/addAssist")
    @ResponseBody
    AjaxRespInfo addAssist(Center assist) {
        AjaxRespInfo ajaxRespInfo = new AjaxRespInfo();
        int rowNum = storeService.addAssist(assist);
        if (rowNum == 1) {
            ajaxRespInfo.setCode(0);
            ajaxRespInfo.setMsg("添加成功!");
        } else {
            ajaxRespInfo.setCode(-1);
            ajaxRespInfo.setMsg("添加失败!");
        }
        return ajaxRespInfo;
    }

    @RequestMapping("/loadCenter")
    @ResponseBody
    AjaxRespInfo<List<Center>> loadCenter(String query, int curr, int limit) {
        AjaxRespInfo<List<Center>> ajaxRespInfo = new AjaxRespInfo<>();
        List<Center> list = storeService.loadCenter(query, curr, limit);
        if (list != null && !list.isEmpty()) {
            ajaxRespInfo.setCode(0);
            ajaxRespInfo.setData(list);
        } else {
            ajaxRespInfo.setCode(1);
            ajaxRespInfo.setMsg("暂无数据");
        }
        return ajaxRespInfo;
    }

    /**
     * 加载二级库入库申请
     */
    @RequestMapping("/loadApp")
    @ResponseBody
    AjaxRespInfo<List<Center>> loadApp() {
        AjaxRespInfo<List<Center>> ajaxRespInfo = new AjaxRespInfo<>();
        List<Center> list = storeService.loadApp();
        if (list != null && !list.isEmpty()) {
            ajaxRespInfo.setCode(0);
            ajaxRespInfo.setData(list);
        } else {
            ajaxRespInfo.setCode(1);
            ajaxRespInfo.setMsg("暂无申请");
        }
        return ajaxRespInfo;
    }

    /**
     * 加载对应二级库借调信息
     */
    @RequestMapping("/loadRent")
    @ResponseBody
    AjaxRespInfo<List<Rent>> loadRent(String kind) {
        AjaxRespInfo<List<Rent>> ajaxRespInfo = new AjaxRespInfo<>();
        List<Rent> list = storeService.loadRent(kind);
        if (list != null && !list.isEmpty()) {
            ajaxRespInfo.setCode(0);
            ajaxRespInfo.setData(list);
        } else {
            ajaxRespInfo.setCode(1);
            ajaxRespInfo.setMsg("暂无申请");
        }
        return ajaxRespInfo;
    }

    @RequestMapping("/getCenter")
    @ResponseBody
    AjaxRespInfo<Center> getCenter(String id) {
        AjaxRespInfo<Center> ajaxRespInfo = new AjaxRespInfo<>();
        Center dbCenter = storeService.getCenter(id);
        if (dbCenter != null) {
            ajaxRespInfo.setCode(0);
            ajaxRespInfo.setData(dbCenter);
        } else {
            ajaxRespInfo.setCode(-1);
            ajaxRespInfo.setMsg("参数错误");
        }
        return ajaxRespInfo;
    }

    @RequestMapping("/getAssist")
    @ResponseBody
    AjaxRespInfo<Center> getAssist(String id) {
        AjaxRespInfo<Center> ajaxRespInfo = new AjaxRespInfo<>();
        Center dbAssist = storeService.getAssist(id);
        if (dbAssist != null) {
            ajaxRespInfo.setCode(0);
            ajaxRespInfo.setData(dbAssist);
        } else {
            ajaxRespInfo.setCode(-1);
            ajaxRespInfo.setMsg("参数错误");
        }
        return ajaxRespInfo;
    }

    @RequestMapping("/modifyCenter")
    @ResponseBody
    AjaxRespInfo modifyCenter(Center center) {
        AjaxRespInfo ajaxRespInfo = new AjaxRespInfo();
        int rowNum = storeService.updateCenter(center);
        if (rowNum == 1) {
            ajaxRespInfo.setCode(0);
            ajaxRespInfo.setMsg("修改成功");
        } else {
            ajaxRespInfo.setCode(-1);
            ajaxRespInfo.setMsg("参数错误");
        }
        return ajaxRespInfo;
    }

    @RequestMapping("/modifyAssist")
    @ResponseBody
    AjaxRespInfo modifyAssist(Center assist) {
        AjaxRespInfo ajaxRespInfo = new AjaxRespInfo();
        int rowNum = storeService.updateAssist(assist);
        if (rowNum == 1) {
            ajaxRespInfo.setCode(0);
            ajaxRespInfo.setMsg("修改成功");
        } else {
            ajaxRespInfo.setCode(-1);
            ajaxRespInfo.setMsg("参数错误");
        }
        return ajaxRespInfo;
    }

    @RequestMapping("/verifyApp")
    @ResponseBody
    AjaxRespInfo verifyApp(Center assist) {
        AjaxRespInfo ajaxRespInfo = new AjaxRespInfo();
        String msg = "";
        if (assist.getDelFlag().equals("1")) {
            int rowNum = storeService.updateAssist(assist);
            msg = rowNum == 1 ? "操作成功" : "参数错误";
        } else {
            msg = storeService.verifyApp(assist);
        }
        ajaxRespInfo.setCode(0);
        ajaxRespInfo.setMsg(msg);
        return ajaxRespInfo;
    }

    @RequestMapping("/verifyRent")
    @ResponseBody
    AjaxRespInfo verifyRent(Rent rent) {
        AjaxRespInfo ajaxRespInfo = new AjaxRespInfo();
        String msg = "";
        if (rent.getDelFlag().equals("1")) {
            int rowNum = storeService.updateRent(rent);
            msg = rowNum == 1 ? "操作成功" : "参数错误";
        } else {
            msg = storeService.verifyRent(rent);
        }
        ajaxRespInfo.setCode(0);
        ajaxRespInfo.setMsg(msg);
        return ajaxRespInfo;
    }

    @RequestMapping("/countCenter")
    @ResponseBody
    AjaxRespInfo<Integer> countCenter(String query) {
        AjaxRespInfo<Integer> ajaxRespInfo = new AjaxRespInfo<>();
        int count = storeService.countCenter(query);
        ajaxRespInfo.setData(count);
        return ajaxRespInfo;
    }

    @RequestMapping("/outCenter")
    @ResponseBody
    AjaxRespInfo<Integer> outCenter(String id) {
        AjaxRespInfo<Integer> ajaxRespInfo = new AjaxRespInfo<>();
        int rowNum = storeService.outCenter(id);
        if (rowNum != 0) {
            ajaxRespInfo.setMsg("出库成功");
        } else {
            ajaxRespInfo.setMsg("系统错误");
        }
        return ajaxRespInfo;
    }

    @RequestMapping("/outAssist")
    @ResponseBody
    AjaxRespInfo<Integer> outAssist(String id, int outNum) {
        AjaxRespInfo<Integer> ajaxRespInfo = new AjaxRespInfo<>();
        int rowNum = storeService.outAssist(id,outNum);
        if (rowNum != 0) {
            ajaxRespInfo.setMsg("出库成功");
        } else {
            ajaxRespInfo.setMsg("非法输入");
        }
        return ajaxRespInfo;
    }

    /**
     * 获取当前系统中各个二级库
     */
    @RequestMapping("/loadAssistList")
    @ResponseBody
    AjaxRespInfo<List<AssistList>> loadAssistList() {
        AjaxRespInfo<List<AssistList>> ajaxRespInfo = new AjaxRespInfo<>();
        List<AssistList> list = storeService.loadAssistList();
        if (list != null && !list.isEmpty()) {
            ajaxRespInfo.setData(list);
            ajaxRespInfo.setCode(0);
        } else {
            ajaxRespInfo.setMsg("系统错误");
            ajaxRespInfo.setCode(-1);
        }
        return ajaxRespInfo;
    }


    /**
     * 加载二级库中数据
     */
    @RequestMapping("/loadAssist")
    @ResponseBody
    AjaxRespInfo<List<Center>> loadAssist(String kind) {
        AjaxRespInfo<List<Center>> ajaxRespInfo = new AjaxRespInfo<>();
        List<Center> list = storeService.loadAssist(kind);
        if (list != null && !list.isEmpty()) {
            ajaxRespInfo.setData(list);
            ajaxRespInfo.setCode(0);
        } else {
            ajaxRespInfo.setMsg("暂无数据");
            ajaxRespInfo.setCode(1);
        }
        return ajaxRespInfo;
    }


    /**
     * 借调二级库
     */
    @RequestMapping("/rentAssist")
    @ResponseBody
    AjaxRespInfo rentAssist(Rent rent) {
        AjaxRespInfo ajaxRespInfo = new AjaxRespInfo<>();
        String msg = storeService.rentAssist(rent);
        ajaxRespInfo.setMsg(msg);
        ajaxRespInfo.setCode(0);
        return ajaxRespInfo;
    }

}
